package org.example.web.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.model.entity.*;
import org.example.model.enums.ItemType;
import org.example.web.admin.mapper.*;
import org.example.web.admin.service.*;
import org.example.web.admin.vo.attr.AttrValueVo;
import org.example.web.admin.vo.graph.GraphVo;
import org.example.web.admin.vo.room.RoomDetailVo;
import org.example.web.admin.vo.room.RoomItemVo;
import org.example.web.admin.vo.room.RoomQueryVo;
import org.example.web.admin.vo.room.RoomSubmitVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    //room_facility：房间配套表
    @Autowired
    private RoomFacilityService roomFacilityService;
    //room_label：房间标签表
    @Autowired
    private RoomLabelService roomLabelService;
    //room_attr_value：房间属性数据表
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    //graph_info：房间图片表
    @Autowired
    private GraphInfoService graphInfoService;
    //room_lease_term：房间租期表
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    //room_payment_type：房间支付方式表
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Override
    public void saveOrUpdateRoomInfo(RoomSubmitVo roomSubmitVo) {
        boolean isUpdate = StrUtil.isEmptyIfStr(roomSubmitVo.getId());
        this.saveOrUpdate(roomSubmitVo);
        if (isUpdate) {
            //删除配套数据
            LambdaUpdateWrapper<RoomFacility> facilityUpdateWrapper = new LambdaUpdateWrapper<>();
            facilityUpdateWrapper.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            roomFacilityService.remove(facilityUpdateWrapper);

            //删除房间标签
            LambdaUpdateWrapper<RoomLabel> labelUpdateWrapper = new LambdaUpdateWrapper<>();
            labelUpdateWrapper.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            roomLabelService.remove(labelUpdateWrapper);

            //删除房间属性
            LambdaUpdateWrapper<RoomAttrValue> attrUpdateMapper = new LambdaUpdateWrapper<>();
            attrUpdateMapper.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            roomAttrValueService.remove(attrUpdateMapper);

            //删除图片
            LambdaUpdateWrapper<GraphInfo> graphInfoUpdateWrapper = new LambdaUpdateWrapper<>();
            graphInfoUpdateWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId());
            graphInfoUpdateWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(graphInfoUpdateWrapper);

            //删除房间租期
            LambdaUpdateWrapper<RoomLeaseTerm> termUpdateWrapper = new LambdaUpdateWrapper<>();
            termUpdateWrapper.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            roomLeaseTermService.remove(termUpdateWrapper);

            //删除房间支付方式
            LambdaUpdateWrapper<RoomPaymentType> paymentTypeUpdateWrapper = new LambdaUpdateWrapper<>();
            paymentTypeUpdateWrapper.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            roomPaymentTypeService.remove(paymentTypeUpdateWrapper);
        }

        //保存新的graphInfoList
        //从vo获取图片列表集合
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        //判断集合是否为空
        if (!CollectionUtils.isEmpty(graphVoList)) {
            List<GraphInfo> list = new ArrayList<>();
            //graphVoList遍历
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                //图片名称
                graphInfo.setName(graphVo.getName());
                //类型 ：公寓 房间
                graphInfo.setItemType(ItemType.ROOM);
                // id
                graphInfo.setItemId(roomSubmitVo.getId());
                //图片地址
                graphInfo.setUrl(graphVo.getUrl());
                //放到list集合
                list.add(graphInfo);
            }
            //批量添加
            graphInfoService.saveBatch(list);
        }

        //保存新的roomAttrValueList
        List<Long> attrValueIdList = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIdList)) {
            List<RoomAttrValue> list = new ArrayList<>();

            for (Long arrtValueId : attrValueIdList) {

                // RoomAttrValue roomAttrValue1 =
                //  RoomAttrValue.builder().attrValueId(arrtValueId)
                //  .roomId(roomSubmitVo.getId()).build();

                RoomAttrValue roomAttrValue = new RoomAttrValue();
                roomAttrValue.setRoomId(roomSubmitVo.getId());
                roomAttrValue.setAttrValueId(arrtValueId);

                list.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(list);
        }

        //3.保存新的facilityInfoList
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            List<RoomFacility> roomFacilityList = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility =
                        RoomFacility.builder().roomId(roomSubmitVo.getId())
                                .facilityId(facilityInfoId).build();
                roomFacilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }

        //4.保存新的labelInfoList
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)) {
            ArrayList<RoomLabel> roomLabelList = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel =
                        RoomLabel.builder().roomId(roomSubmitVo.getId())
                                .labelId(labelInfoId).build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }

        //5.保存新的paymentTypeList
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
            ArrayList<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType =
                        RoomPaymentType.builder().roomId(roomSubmitVo.getId())
                                .paymentTypeId(paymentTypeId).build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }

        //6.保存新的leaseTermList
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)) {
            ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm =
                        RoomLeaseTerm.builder().roomId(roomSubmitVo.getId())
                                .leaseTermId(leaseTermId).build();
                roomLeaseTerms.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }
    }

    @Override
    public IPage<RoomItemVo> selectRoomInfoPage(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        IPage<RoomItemVo> pageModel = roomInfoMapper.selectRoomInfoPage(page, queryVo);
        return pageModel;
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomInfo roomInfo = getById(id);
        if (roomInfo == null) {
            return null;
        }

        Long apartmentId = roomInfo.getApartmentId();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(apartmentId);
        //配套
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.findFacilityListByRoomId(id);
        //标签
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);
        //房间属性
        List<AttrValueVo> attrvalueVoList = attrValueMapper.selectListByRoomId(id);
        //房间图片
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphListByRoomId(ItemType.ROOM, id);
        //租期
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);
        //支付方式
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);

        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);

        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setAttrValueVoList(attrvalueVoList);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        return roomDetailVo;
    }

    @Override
    public void removeRoomInfo(Long id) {
        //存在租期时不能删除房间
        LambdaQueryWrapper<RoomLeaseTerm> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoomLeaseTerm::getRoomId, id);
        //状态是2.5
        wrapper.in(RoomLeaseTerm::getRoomId, 2, 5);
        long count = roomLeaseTermService.count(wrapper);
        if (count > 0) { //存在房间信息
            throw new RuntimeException("存在房间租期信息，不能删除");
        }
        //删除配套
        LambdaUpdateWrapper<RoomFacility> facilityQueryWrapper =  new LambdaUpdateWrapper<>();
        facilityQueryWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(facilityQueryWrapper);
        //删除标签
        LambdaUpdateWrapper<RoomLabel> labelQueryWrapper = new LambdaUpdateWrapper<>();
        labelQueryWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(labelQueryWrapper);
        //删除属性
        LambdaUpdateWrapper<RoomAttrValue> attrValueQueryWrapper = new LambdaUpdateWrapper<>();
        attrValueQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(attrValueQueryWrapper);
        //删除图片
        LambdaUpdateWrapper<GraphInfo> graphQueryWrapper = new LambdaUpdateWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemId, id);
        graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphInfoService.remove(graphQueryWrapper);
        //删除租期
        LambdaUpdateWrapper<RoomLeaseTerm> leaseTermQueryWrapper = new LambdaUpdateWrapper<>();
        leaseTermQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(leaseTermQueryWrapper);
        //删除支付方式
        LambdaUpdateWrapper<RoomPaymentType> paymentTypeQueryWrapper = new LambdaUpdateWrapper<>();
        paymentTypeQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(paymentTypeQueryWrapper);
    }

    @Override
    public List<RoomInfo> listInfoById(Long id) {
        List<RoomInfo> list = roomInfoMapper.listInfoById(id);
        return list;
    }
}




